Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plausible: init at 1.3.0 #124055

Merged
merged 9 commits into from
Jun 2, 2021
Merged

plausible: init at 1.3.0 #124055

merged 9 commits into from
Jun 2, 2021

Conversation

Ma27
Copy link
Member

@Ma27 Ma27 commented May 22, 2021

Motivation for this change

plausible is a simple and privacy-friendly alternative to Google Analytics based on Elixir.

cc @happysalada @lheckemann @kvz

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Added a release notes entry if the change is major or breaking
  • Fits CONTRIBUTING.md.

@r-rmcgibbo
Copy link

r-rmcgibbo commented May 22, 2021

Result of nixpkgs-review pr 124055 at 9eb694c5 run on aarch64-linux 1

1 package failed to build:
  • nixos-install-tools
1 package built successfully:
  • plausible
4 suggestions:
  • warning: unused-argument

    Unused argument: glibcLocales.
    Near pkgs/servers/web-apps/plausible/default.nix:1:47:

      |
    1 | { lib, stdenv, beamPackages, fetchFromGitHub, glibcLocales, cacert
      |                                               ^
    
  • warning: unused-argument

    Unused argument: stdenv.
    Near pkgs/servers/web-apps/plausible/default.nix:1:8:

      |
    1 | { lib, stdenv, beamPackages, fetchFromGitHub, glibcLocales, cacert
      |        ^
    
  • warning: unused-argument

    Unused argument: cacert.
    Near pkgs/servers/web-apps/plausible/default.nix:1:61:

      |
    1 | { lib, stdenv, beamPackages, fetchFromGitHub, glibcLocales, cacert
      |                                                             ^
    
  • warning: fixup-phase

    fixupPhase should not be overridden, use postFixup instead.

    Near pkgs/development/beam-modules/mix-release.nix:77:3:

       |
    77 |   fixupPhase = ''
       |   ^
    

Note that build failures may predate this PR, and could be nondeterministic or hardware dependent.
Please exercise your independent judgement. Does something look off? Please file an issue or reach out on IRC.


Result of nixpkgs-review pr 124055 at 9eb694c5 run on x86_64-linux 1

1 package failed to build:
  • nixos-install-tools
1 package built successfully:
  • plausible
4 suggestions:
  • warning: unused-argument

    Unused argument: stdenv.
    Near pkgs/servers/web-apps/plausible/default.nix:1:8:

      |
    1 | { lib, stdenv, beamPackages, fetchFromGitHub, glibcLocales, cacert
      |        ^
    
  • warning: unused-argument

    Unused argument: glibcLocales.
    Near pkgs/servers/web-apps/plausible/default.nix:1:47:

      |
    1 | { lib, stdenv, beamPackages, fetchFromGitHub, glibcLocales, cacert
      |                                               ^
    
  • warning: fixup-phase

    fixupPhase should not be overridden, use postFixup instead.

    Near pkgs/development/beam-modules/mix-release.nix:77:3:

       |
    77 |   fixupPhase = ''
       |   ^
    
  • warning: unused-argument

    Unused argument: cacert.
    Near pkgs/servers/web-apps/plausible/default.nix:1:61:

      |
    1 | { lib, stdenv, beamPackages, fetchFromGitHub, glibcLocales, cacert
      |                                                             ^
    

Note that build failures may predate this PR, and could be nondeterministic or hardware dependent.
Please exercise your independent judgement. Does something look off? Please file an issue or reach out on IRC.

''} plausible";
};
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be a need to add a service for a remote shell ?
In order to be able to connect to a running instance, you need to /bin/plausible remote with the necessary environment variable.
I'm just thinking it might be a very nice convenience in order to debug or check what is happening. It definitely might not be useful for the average user.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I guess it's nicer though to have a wrapper-script for that which can be executed from the shell. An example for what I mean would be nextcloud-occ.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good! I'm not sure I understand the way secrets are managed, but otherwise it looks good!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm even if the environment is correctly set up, plausible remote complains about a missing HOME dir even though it's set (checked by looking at /proc/pid/environ)... will take a look at this later.

Copy link
Member

@aanderse aanderse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module LGTM and I approve as is, though as mentioned I think passing a few files to EnvironmentFile instead of the accepting situation might be beneficial... but hey that's just my opinion.

I don't know anything about elixir packaging so maybe someone else can take a look at the package portion of this PR.

nixos/modules/services/web-apps/plausible.nix Show resolved Hide resolved
nixos/modules/services/web-apps/plausible.nix Outdated Show resolved Hide resolved
nixos/modules/services/web-apps/plausible.nix Show resolved Hide resolved
@Ma27
Copy link
Member Author

Ma27 commented May 23, 2021

Thanks a lot for the reviews folks! Will work on all that next week :)

Copy link
Member

@lheckemann lheckemann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

nixos/modules/services/web-apps/plausible.nix Outdated Show resolved Hide resolved
nixos/modules/services/web-apps/plausible.nix Outdated Show resolved Hide resolved
nixos/modules/services/web-apps/plausible.nix Outdated Show resolved Hide resolved
nixos/modules/services/web-apps/plausible.nix Outdated Show resolved Hide resolved
pkgs/servers/web-apps/plausible/update.sh Outdated Show resolved Hide resolved
pkgs/servers/web-apps/plausible/update.sh Outdated Show resolved Hide resolved
pkgs/servers/web-apps/plausible/update.sh Outdated Show resolved Hide resolved
pkgs/servers/web-apps/plausible/update.sh Outdated Show resolved Hide resolved
pkgs/servers/web-apps/plausible/update.sh Outdated Show resolved Hide resolved
pkgs/servers/web-apps/plausible/update.sh Outdated Show resolved Hide resolved
@@ -0,0 +1,51 @@
<chapter xmlns="http://docbook.org/ns/docbook"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert nixos docs to markdown.

Ma27 added 2 commits May 29, 2021 16:12
Needed to modify e.g. a few things in the `plausible` package to get it
running properly.
* Most significant is probably the patching necessary to run plausible
  with postgres without superuser privilege. This change includes:
  * updating ecto_sql to 3.6 where `CREATE DATABASE` is only executed if
    it doesn't exist[1].
  * patching a migration to only modify the `users.email` column (to use
    `citext` rather than creating the extension. `plausible-postgres`
    takes care of that).
* Correctly declare dependencies in systemd.
* A few minor fixes.

[1] elixir-ecto/ecto_sql@051baf6
Ma27 and others added 2 commits May 31, 2021 19:01
Co-authored-by: Raphael Megzari <raphael@megzari.com>
@Ma27 Ma27 requested review from happysalada and aanderse June 1, 2021 16:05
};
server = {
baseUrl = "http://localhost:8000";
secretKeybaseFile = "${pkgs.writeText "dont-try-this-at-home" "nannannannannannannannannannannannannannannannannannannan_batman!"}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Copy link
Contributor

@happysalada happysalada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good to me.
A couple of comments added would be great but not an unbendable requirement.

What about the doc issue? I don't know anything about xml vs markdown, I'm willing to go either way, do we want to address the issue in this PR?

@Ma27
Copy link
Member Author

Ma27 commented Jun 2, 2021

What about the doc issue? I don't know anything about xml vs markdown, I'm willing to go either way, do we want to address the issue in this PR?

AFAICS there isn't really much migrated, so it's not clear to me how I can do the fancy stuff such as referencing options in code-examples or callout-lists in markdown. Probably cheaper to run pandoc once anyways 🤷

I'm not sure you need this patch. If you create the extension before hand, it won't try to create it again.

I think that this wasn't sufficient, but I have a new theory why. Gotta check if I can remove it :)

Turns out this isn't needed if the module correctly adds the `citext`
extension to the `plausible` database.
@Ma27 Ma27 requested a review from happysalada June 2, 2021 09:48
@happysalada happysalada merged commit c4afcbb into NixOS:master Jun 2, 2021
@happysalada
Copy link
Contributor

Thanks a lot for doing this!

@Ma27 Ma27 deleted the plausible branch June 2, 2021 10:36
@nh2
Copy link
Contributor

nh2 commented Jul 12, 2021

Hi, I'm currently trying this out.

How is one supposed to set secretKeybaseFile in production?

Because DynamicUser is used, I cannot know the user ID ahead of time that will need to read that file, and I certainly don't want to put it world-readable in the nix store.

Are there any pointers I could read about how one does that?

@lheckemann
Copy link
Member

Off the top of my head, I think LoadCredential= (see systemd.exec(5)) could be helpful for this, but I'm not sure if applying it here is trivial or not.

@Ma27
Copy link
Member Author

Ma27 commented Jul 12, 2021

I'm not sure right now, but IIRC it was sufficient to set e.g. SupplementaryGroups and make the secret readable by some sort of dedicated group (which is also the default approach used by e.g. deployment.keys of nixops or sops-nix). If you're having problems with this, I can check e.g. tomorrow :)


# FIXME consider using LoadCredential as soon as it actually works.
envSecrets = ''
export ADMIN_USER_PWD="$(<${cfg.adminUser.passwordFile})"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As pretty much 100% of shell scripts, this is probably wrong:

The shell this runs in doesn't have set -e nor any other reasonable way of error propagation, so when the configured file isn't readable by the DynamicUser, this code just prints e.g.

/nix/store/rwfwdk34fyz9qqyvn4vs0lnswscd73x0-plausible-setup: line 2: /var/lib/plausible/plausible-admin-user-password-file: Permission denied
/nix/store/rwfwdk34fyz9qqyvn4vs0lnswscd73x0-plausible-setup: line 3: /var/lib/plausible/plausible-secret-keybase-file: Permission denied

and then happily goes on with its day, with ADMIN_USER_PWD and SECRET_KEY_BASE set to the empty string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if it had set -e, it would still be wrong, in the same way as the scripts of Elixir itself here:

elixir-lang/elixir#11114

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes in PR #130062

psql -d plausible <<< "UPDATE users SET email_verified=true;"
fi
''}
''} plausible-setup";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the argument to this shell script for?

It calls e.g.

/nix/store/skpa6a2l84ydidgxnpv63fnc1fsxyv6k-plausible-setup plausible-setup

But the shell script defined directly above does not use this argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To quote systemd.service(5):

If the executable path is prefixed with "@", the second specified
token will be passed as "argv[0]" to the executed process (instead of
the actual filename), followed by the further arguments specified.

A similar approach for shorter names is done in e.g. the module of Hydra or the firewall :)

Does that answer your question?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually wanted to ask you why you used @ in this case? (rather without)
You don't actually use the argument in this case right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ma27 Ah yes, thank you! Is the intent to make it look better in ps/htop?

ExecStart = "@${pkgs.writeShellScript "plausible" ''
${envSecrets}
plausible start
''} plausible";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@happysalada
Copy link
Contributor

The "best" way to do this would be to use LoadCredentials.
The current problem is that systemd is blocked at version 147 and LoadCredentials doesn't work with that version.
The other problem is to make upstream be able to read a configuration variable from a file. I've worked with upstream for that change, and it should be available in the next release.

So in summary no perfect way to do this. I haven't found a way without LoadCredentials to not have your variable readable.

@nh2
Copy link
Contributor

nh2 commented Jul 13, 2021

@happysalada Another problem I have with DynamicUser is that I cannot figure out how to debug things well with it.

For me the plausible startup fails with

** (RuntimeError) Code: 81, e.displayText() = DB::Exception: Database `` doesn't exist (version 20.5.2.7)

because in some of its calls from createdb.sh, it seems to not pass the CLICKHOUSE_DATABASE_URL=http://127.0.0.1:8123/default setting all the way through, and make ?database= requests (with empty string as the DB name), right after making two ?database=default requests.

The usual approach is to sudo -u plausible /path/to/createdb.sh, running it directly, perhaps with strace.

This seems impossible with DynamicUser, because there is no user you could sudo -u as.

So while I like the concept of DynamicUser, it seems to currently block these more fundamental debugging / sysadmin facilities.

@Ma27
Copy link
Member Author

Ma27 commented Jul 13, 2021

You should be able to do something like systemd-run -p DynamicUser=yes -p StateDirectory=plausible -- your cmd. If a StateDirectory is used, the DynamicUser will be named after and then you have an environment similar to the service itself.

@happysalada
Copy link
Contributor

I love this conversation!
I originally had some problems with DynamicUser for the backup. I ended up creating another User for backup and granting all priviledges on db for it.
I don't really like having to create another user and granting all priviledges. Is there some other black magic command from systemd that has something for that?
Or do you guys also have another user for backups on production?
(I use a systemd-timer, with a simple shell script that basically dumps the db, gathers other files like uploads and the likes and borgbackup it to a remote)


# We need ecto 3.6 as this version checks whether the database exists before
# trying to create it. The creation attempt would always require super-user privileges
# and since 3.6 this isn't the case anymore.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants